const redis = require('redis')
const config = require('@/config')

class RedisClient {
    constructor() {
        this.client = redis.createClient({ url: 'redis://127.0.0.1:6379' })
        // 错误监听
        this.client.on('error', err => {
            console.log('redis错误')
            console.log(err.message || "")
        })
        // 
        this.client.on('ready', () => {
            console.log('redis 已连接...')
        })
        // 连接
        this.client.connect()
    }

    // 增 expireIn 单位s
    set(key, val, expireIn = null) {
        this.client.set(key, val)
        if (expireIn) this.client.expire(key, expireIn)
    }
    // 查
    get(key) {
        return this.client.get(key)
    }
    // 删
    remove(key) {
        this.client.del(key)
    }
    // 关闭
    close() {
        this.client.quit()
    }
}

module.exports = new RedisClient()
